package com.bodacn.keypoint;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

public class ImageActivity extends AppCompatActivity {
    PhotoViewAttacher mAttacher;
    PhotoView photoView;
    String PhotoUrl="";
    Button btn_ImageClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        //获取登陆参数
        Bundle paramBundle = this.getIntent().getExtras();
        PhotoUrl=paramBundle.getString("PhotoUrl");
        InitView();
    }

    private void InitView(){
        btn_ImageClose=(Button)findViewById(R.id.btn_ImageClose);
        btn_ImageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("onClick","准备关闭");
                //关闭当前Activity;
                finish();
            }
        });
        photoView=(PhotoView)findViewById(R.id.photoView);
        if (PhotoUrl.length()>0) {
            //读取本地图片;
            photoView.setImageBitmap(getLoacalBitmap(PhotoUrl));
            mAttacher = new PhotoViewAttacher(photoView);
            mAttacher.update();
        }
    }


    /**
     * @Title: getLoacalBitmap
     * @Description: 加载本地图片
     * @param @param url 本地路径
     * @param @return
     * @return Bitmap
     * @throws
     */
    public Bitmap getLoacalBitmap(String url) {
        if (url != null) {
            FileInputStream fis=null;
            try {
                fis = new FileInputStream(url);
                return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return null;
            }finally{
                if(fis!=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    fis=null;
                }
            }
        } else {
            return null;
        }
    }

}
